import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg'

const noJwtPages = [
  '/api/login',
  '/api/getSessionCsrfToken',
  '/api/getUserList',
]

export default (appInfo: EggAppInfo) => {
  const config = {
    security: {
      csrf: {
        // headerName: 'x-csrf-token',
        // useSession: true, // 开启后，可以在ctx.session获取到csrfToken
        enable: false,
      },
    },
    jwt: {
      secret: 'buding', // TODO 加密字符串
    },
  } as PowerPartial<EggAppConfig>

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1672035471544_1811'

  // add your egg config in here
  config.middleware = ['tokenHandler', 'resHandler']

  // add your special config in here
  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
  }

  config.sequelize = {
    dialect: 'mysql',
    host: 'http://localhost', // TODO 填上自己服务器的ip地址
    username: 'root',
    password: 'TODO', // 密码
    port: 3306,
    database: 'eggServer', // TODO 数据库名称
    timezone: '+08:00', // 表示为东八区的时间
  }

  // the return config will combines to EggAppConfig
  return {
    ...config,
    ...bizConfig,
    tokenHandler: {
      match(ctx) {
        const url = ctx.request.url
        if (noJwtPages.includes(url)) {
          return false
        }
        return true
      },
    },
  }
}
